package ucd.csi.comp30220.fall2011.oteop.demo;

import java.util.ArrayList;

import ucd.csi.comp30220.fall2011.oteop.flight.FlightInfo;
import ucd.csi.comp30220.fall2011.oteop.flight.FlightStatus;
import ucd.csi.comp30220.fall2011.oteop.flight.FlightStatusListener;
import ucd.csi.comp30220.fall2011.oteop.flight.FlightStatusSubscriber;
import ucd.csi.comp30220.fall2011.oteop.flight.PlaneInfo;
import ucd.csi.comp30220.fall2011.oteop.flight.impl.RemoteFlightServer;
import ucd.csi.comp30220.fall2011.oteop.flughafen.AirportInfo;
import ucd.csi.comp30220.fall2011.oteop.navigation.Position;

public class DublinHeathrow {
    
    public static final Position DUB = new Position(53.42155, -6.2977);
    public static final Position LHR = new Position(51.4706, -0.4619416);
    public static final Position NCL = new Position(55.0375, -1.6917);
    public static final Position EDI = new Position(55.9500, -3.35007);
    
    protected static PlaneInfo createPlaneInfo(int multiplier) {
        return new PlaneInfo(
            "Boeing 747-200",   // type
            45100,              // service ceiling
            35000,              // cruising altitude,
             2000 * multiplier, // rate of climb,
              522 * multiplier, // maximum speed
              100 * multiplier, // stall speed,
              490 * multiplier, // cruising speed
              180 * multiplier, // rotate speed,
              160 * multiplier, // approach speed,
              154 * multiplier, // landing speed,
             4880,              // range
            10500 / multiplier, // take off distance,
             6170 / multiplier  // landing distance
        );
    }
    
    private static final int MULT = 20;

    public static void main(String[] argv) throws Exception {
        
        final int mult = argv.length > 0 ? Integer.parseInt(argv[0]) : MULT;
        
        System.err.println("Starting...");
        
        final ArrayList<RemoteFlightServer> flights = new ArrayList<RemoteFlightServer>();
        
        new Thread(new Runnable() {
            public void run() {
                try {
                    RemoteFlightServer flight = new RemoteFlightServer(
                        new FlightInfo(
                            "British Airways", 
                            "BA999-"+mult,
                            "Speedbird niner niner niner", 
                            createPlaneInfo(mult), 
                            null,
                            new AirportInfo("LHR", LHR), 
                            new AirportInfo("EDI", EDI)));
                    flights.add(flight);
                    flight.start();
                    System.err.println("Flight Server started");
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }}).start();
        
        new Thread(new Runnable() {
            public void run() {
                try {
                    RemoteFlightServer flight = new RemoteFlightServer(
                        new FlightInfo(
                            "British Airways", 
                            "BA23-"+mult,
                            "Speedbird two three", 
                            createPlaneInfo(mult), 
                            null,
                            new AirportInfo("NCL", NCL), 
                            new AirportInfo("DUB", DUB)));
                    flights.add(flight);
                    flight.start();
                    System.err.println("Flight Server started");
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }}).start();

        new Thread(new Runnable() {
            public void run() {
                try {
                    RemoteFlightServer flight = new RemoteFlightServer(
                        new FlightInfo(
                            "British Airways", 
                            "BA47-"+mult,
                            "Speedbird four seven heavy", 
                            createPlaneInfo(mult), 
                            null,
                            new AirportInfo("DUB", DUB), 
                            new AirportInfo("LHR", LHR)));
                    flights.add(flight);
                    flight.start();
                    System.err.println("Flight Server started");
                }catch (Exception e) {
                   e.printStackTrace(); 
                }
            }}).start();
        
        FlightStatusSubscriber subscriber = new FlightStatusSubscriber();
        System.err.println("Got status subscriber");
        Thread.sleep(10*1000);
        
        final long[] times = new long[1];
        times[0] = System.currentTimeMillis();
        subscriber.addListener(new FlightStatusListener() {
            public void onStatusUpdate(FlightStatus status) {
                times[0] = System.currentTimeMillis();
            }
        });
        System.err.println("Waiting");
        while (times[0] > System.currentTimeMillis()-10*1000) {
            Thread.sleep(10*1000);
        }
        subscriber.stop();
        for (RemoteFlightServer flight : flights) {
            flight.stop();
            flight.unregister();
        }
        System.exit(0);
    }
}
